package com.onpositive.queryeditor.storaging;

import java.net.MalformedURLException;
import java.net.URL;
import org.eclipse.core.runtime.Platform;
import org.eclipse.ui.PlatformUI;



public class WorkspaceLocation
{					
	
	
	private static final String FILE_PROTOCOL = "file";
	//protected static Preferences  preferences = Preferences.userNodeForPackage(WorkspaceLocation.class); //TODO UserRoot
	
	protected static final String _KeyWorkspaceRootDir   = "wsRootDir";
	protected static final String _KeyRememberWorkspace  = "wsRemember";
	
	public static URL getWorkspaceURL()
    {
		try
		{
			if (!Platform.isRunning()) {
				String userDir = System.getProperty("user.home");
				if (userDir == null)
					userDir = "C:\\tmp";
				return new URL(FILE_PROTOCOL,null,userDir);
			}
			
	    	String path = PlatformUI.getPreferenceStore().getString(_KeyWorkspaceRootDir);
	    	if (path == null) {
	    		path = Platform.getInstallLocation().getURL().getPath();
				return new URL(FILE_PROTOCOL,null,path);
			}
		}
    	catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
		return null;
    }
    
    public static void setWorkspaceURL(URL url)
    {
    	PlatformUI.getPreferenceStore().setValue(_KeyWorkspaceRootDir, url.getPath());    	
    }
}
